﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GOFconsole
{
    //运用OOP的思想去编写一个简单的计算器
    //要可扩展，可维护
    public class Chapter1_Calculator
    {
        public void OutputFunc_C1()
        {
            Operation oper = null;

            try
            {
                Console.Write("请输入数字1：");
                double numberA = Convert.ToDouble(Console.ReadLine());
                Console.Write("请输入运算符：");
                string operate = Console.ReadLine().ToString();
                Console.Write("请输入数字2：");
                double numberB = Convert.ToDouble(Console.ReadLine());                

                oper = OperationFactory.createOperation(operate);
                oper.numberA = numberA;
                oper.numberB = numberB;

                Console.WriteLine("结果为：" + oper.GetResult());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

        }
    }

    //运算类基类
    class Operation
    {
        public double numberA { get; set; }
        public double numberB { get; set; }

        public virtual double GetResult()
        {
            double result = 0;
            return result;
        }
    }

    #region 具体实现的运算类

    class OperationAdd : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = numberA + numberB;
            return result;
        }
    }

    class OperationMinus : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = numberA - numberB;
            return result;
        }
    }

    class OperationMul : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = numberA * numberB;
            return result;
        }
    }

    //Division 除法
    class OperationDiv : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            if (numberB == 0)
            {
                throw new Exception("除数不能为0！");
            }

            result = numberA / numberB;
            return result;
        }
    }

    #endregion 

    
    //***简单工厂模式***
    //如何让计算器知道，希望使用哪一个运算方法呢？
    //用一个单独的类做这个创造实例的过程，这就是工厂
    class OperationFactory
    {
        public static Operation createOperation(string operate)
        {
            Operation oper = null;
            switch (operate)
            { 
                case "+":
                    oper = new OperationAdd();
                    break;
                case "-":
                    oper = new OperationMinus();
                    break;
                case "*":
                    oper = new OperationMul();
                    break;
                case "/":
                    oper = new OperationDiv();
                    break;
                default:
                    throw new Exception("不支持此操作符！");                    
            }
            return oper;
        }
    }
   


}
